// pages/purchase/purchase.js
var session = require('../../common/session');
var config = require('../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: null,
    toggleObj:{},
    toggleSearch: false,
    startTime:'',
    endTime:'',
    filter:'',
    totalCount:0,
    SkipCount:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAllProcessingOrderDto();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: '采购订单'
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  getAllProcessingOrderDto:function(){
    wx.showLoading({
      title: '加载中...',
    });
    let _this = this;
    let _startDate = _this.data.startTime,
        _endDate = _this.data.endTime;
    wx.request({
      url: config.service.GetAllProcessingOrderDto,
      data: {
        StartDate: _startDate,
        EndDate: _endDate,
        OrderStatus: '',
        Sturt: 2,
        IsSupplier: true,
        Filter:_this.data.filter,
        MaxResultCount: 10,
        SkipCount: _this.data.SkipCount
      },
      method: 'GET',
      header: {
        'Accept': 'application/json',
        'Authorization': 'Bearer ' + session.get('xyznzzaccessToken')
      },
      success: function (res) {
        if(res.data && res.data.result && res.data.result.items && res.data.result.items instanceof Array){
          if(res.data.result.items.length == 0 && _this.data.SkipCount == 0) {
            _this.setData({
              result: res.data.result.items,
              totalCount: res.data.result.totalCount
            });
            return;
          }
          let dataRes = _this.data.result;
          if(!dataRes) dataRes = [];
          _this.setData({
            result: dataRes.concat(res.data.result.items),
            totalCount: res.data.result.totalCount
          })
        } else if(res.statusCode == '401'){
          wx.showToast({
            title: '登录失效，请重新登录',
            icon: 'none',
            duration: 2000,
            mask: true,
            success: function () {
              session.allClear();
              setTimeout(function(){
                wx.reLaunch({
                  url: 'pages/syslogin/syslogin'
                })
              },2000);
             
            }
          });
        }
      },
      fail: function (res) {
        console.log(res)
      },
      complete:function(){
        wx.hideLoading();
      }
    });
  },
  toggleShowOrderList:function(e){
    let _id = e.currentTarget.dataset.id;
    let toggleObj = this.data.toggleObj;
    if(this.data.toggleObj[_id]) {
      toggleObj[_id] = false;
      this.setData({
        toggleObj: toggleObj
      })
    } else {
      toggleObj[_id] = true;
      this.setData({
        toggleObj: toggleObj
      })
    }
  },
  toggleSearch:function(){
    this.setData({
      toggleSearch: !this.data.toggleSearch
    })
  },
  searchFun:function(){
    this.getAllProcessingOrderDto();
  },
  searchInput:function(e){
    this.setData({
      filter: e.detail.value
    })
  },
  searchStartInput:function(e){
    this.setData({
      startTime: e.detail.value
    })
  },
  searchEndInput:function(e){
    this.setData({
      endTime: e.detail.value
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let _SkipCount = this.data.SkipCount;
    if(_SkipCount < (this.data.totalCount - 10)){
      _SkipCount += 10;
      this.setData({
        SkipCount:_SkipCount
      });
      this.getAllProcessingOrderDto();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})